﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using SGLib;
using SGLisp;
public class Unit : MonoBehaviour
{
    UnitHeadBar mHeadBar;
    public long GUID;
    Hashtable mTabData;
    Animator mAnimator;
    Dictionary<string, Ability> mAbilitys = new Dictionary<string, Ability>();
    public Dictionary<string, Ability> Abilitys { get { return mAbilitys; } }
    public Ability CurAbility;
    List<Actor> mActors = new List<Actor>();
    List<Mover> mMovers = new List<Mover>();

    protected Ability mNormalAttack;
    public Ability NormalAttack { get { return mNormalAttack; } }
    public int State;
    EventContainer mEvent = new EventContainer();
    public EventContainer Event { get { return mEvent; } }

    Context mContext;
    public Context Context { get { return mContext; } }
    /// <summary>
    /// 单位属性
    /// </summary>
    Dictionary<string, object> mPropertys = new Dictionary<string, object>();
    /// <summary>
    /// 移动控制器
    /// </summary>
    UnitMoverController mUnitMoverController;
    public UnitMoverController MoverController { get { return mUnitMoverController; } }


    public void SetProperty(string key, object value)
    {
        if (!this.mPropertys.ContainsKey(key))
        {
            this.mPropertys.Add(key, value);
        }
        else
        {
            this.mPropertys[key] = value;
        }
        this.Event.CallEvent("Unit.PropertyChange", key, value);
    }

    public T GetProperty<T>(string key)
    {
        if (mPropertys.ContainsKey(key))
        {
            return (T)mPropertys[key];
        }
        return default(T);
    }

    public static Unit Create(Hashtable tabData, long guid)
    {
        GameObject newUnitG = new GameObject((string)tabData[":name"]);
        Unit newUnit = newUnitG.AddComponent<Unit>();
        newUnit.Init(tabData, guid);
        return newUnit;
    }


    public void Init(Hashtable tabData, long guid)
    {
        mUnitMoverController = gameObject.AddComponent<UnitMoverController>();
        mContext = new Context(Game.Inst.Context);
        mAnimator = gameObject.GetComponent<Animator>();
        GUID = guid;
        mTabData = tabData;
        //加载Ability
        if (tabData.ContainsKey(":abilitys"))
        {
            object[] abilitys = (object[])tabData[":abilitys"];
            for (int i = 0; i < abilitys.Length; i++)
            {
                string abilityName = (string)abilitys[i];
                Ability newAbility = AbilityManager.Instance.Create(abilityName);
                mAbilitys.Add(abilityName, newAbility);
                if (abilityName.IndexOf("普攻") > 0)
                {
                    this.mNormalAttack = newAbility;
                }
            }
        }
        //加载Actor
        if (tabData.ContainsKey(":actors"))
        {
            object[] actors = (object[])tabData[":actors"];
            for (int i = 0; i < actors.Length; i++)
            {
                string actorName = (string)actors[i];
                Actor newActor = Actor.Create(actorName, this);
                this.mActors.Add(newActor);
            }
        }
        //属性
        if (tabData.ContainsKey(":propertys"))
        {
            Hashtable propertyTable = (Hashtable)tabData[":propertys"];
            foreach (DictionaryEntry dicItem in propertyTable)
            {
                mPropertys.Add((string)dicItem.Key, dicItem.Value);
            }
            mUnitMoverController.Init(GetProperty<int>(":MoveSpeed"));
        }
        if (this.mPropertys.ContainsKey(":MaxHP"))
        {
            this.SetProperty(":HP", this.GetProperty<float>(":MaxHP"));
        }
        if (this.mPropertys.ContainsKey(":MaxStamina"))
        {
            this.SetProperty(":Stamina", this.GetProperty<float>(":MaxStamina"));
        }
        if (tabData.ContainsKey(":hero?") && (bool)tabData[":hero?"])
        {
            mHeadBar = Instantiate(Resources.Load<GameObject>("Unit/UnitHeadBar")).GetComponent<UnitHeadBar>();
            mHeadBar.transform.SetParent(transform, false);
            mHeadBar.Init(SGLib.Scene.SceneManager.Instance.Cur<GameScene>().GameCamera.transform, this);
        }

        this.hitFlyMover = new LineMover();
        this.hitFlyMover.MoveDistance = 3;
        this.hitFlyMover.Speed = 15;
        
        this.AddMover(hitFlyMover);
        this.OnLoad();
    }

    public void AddMover(Mover mover)
    {
        mMovers.Add(mover);
    }

    public virtual void OnLoad()
    {
        this.Event.CallEvent("Unit.OnLoad");
    }

    public void ServerUpdate(long dt)
    {
        if (mUnitMoverController != null)
        {
            mUnitMoverController.ServerUpdate(dt);
        }
        for (int i = 0; i < this.mMovers.Count; i++)
        {
            this.mMovers[i].ServerUpdate(dt);
        }

    }

    LineMover hitFlyMover = new LineMover();
    public void HitFly(long startTime, int startX,int startZ,int dirX,int dirZ)
    {
        hitFlyMover.Dir = new Vector3(0,0,1);
        hitFlyMover.StartTime = startTime;
        hitFlyMover.StartPos = new Vector3(startX/100f,this.transform.position.y,startZ/100f);
        this.hitFlyMover.Dir = new Vector3(dirX/100f, this.transform.position.y, dirZ/100f);
        hitFlyMover.Start(this);
        
       
    }
 

    public void OnDestroy()
    {
        this.mUnitMoverController = null;
        this.mMovers.Clear();
        this.Abilitys.Clear();
    }
}
